#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : day_report_business_service.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-08-12 15:18 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from report_system.utils import excel_util
from report_system.utils import mail_util
from report_system.utils import mailbox_util
from report_system.utils.num_util import divi
from report_system.utils.enum_util import DAY_REPORT_BUSINESS_COLOR
from report_system.utils.channel_util import TUJIA, ZHENGUO, AIRBNB
from report_system.report_specify.business_specify import *
from report_system.report_specify.service_specify import im_online_day, online_refund_rate_month
from report_system.utils.development_util import debug

if debug:
    receivers = 'dt@iyoujia.com;wujiming@iyoujia.com'
    city_mails = mailbox_util.mails_test
else:
    receivers = 'yitangf@sweetome.com;yangweiwei@iyoujia.com;machao@iyoujia.com;' \
                'wangjing@iyoujia.com;wangjian@iyoujia.com;dt@iyoujia.com;wangchao@iyoujia.com;' \
                'yy@iyoujia.com;ccsj@iyoujia.com;zhuqiang@iyoujia.com;lvweixin@iyoujia.com'
    city_mails = mailbox_util.mails_all


def process(st=None, et=None):
    today = date_util.curdate(0)
    yesterday = date_util.curdate()
    st = st if st else date_util.cur_month_first(days=1, dt=yesterday)
    et = et if et else yesterday
    online_today_df = online_sale_data(today, today)
    im_online_today_df = im_online_day(st=today, et=today)

    today_df = pd.merge(online_today_df, im_online_today_df, how='left')
    today_df = df_util.df_set_index(today_df, ["城市", "门店", "门店销售"])
    today_df = df_util.df_set_first_title(today_df, "今日数据")
    log.info("今日数据准备完毕。。。。。。。")

    online_yesterday_df = online_sale_data(yesterday, yesterday)
    im_online_yesterday_df = im_online_day(st=yesterday, et=yesterday)
    yesterday_df = pd.merge(online_yesterday_df, im_online_yesterday_df, how='left')
    yesterday_df = df_util.df_set_index(yesterday_df, ["城市", "门店", "门店销售"])
    yesterday_df = df_util.df_set_first_title(yesterday_df, "昨日数据")
    log.info("昨日数据准备完毕。。。。。。。")
    result_df = pd.merge(yesterday_df, today_df, how='left', left_index=True, right_index=True)
    log.info("合并昨日数据、今日数据完毕。。。。。。。")

    month_order_df = month_order_online(st=st, et=yesterday)
    month_order_df = df_util.df_set_index(month_order_df, ["城市", "门店销售"])
    log.info("月累计评论准备完毕。。。。。。。")
    online_month_df = online_sale_data(st, yesterday)
    # online_month_df['套均GMV'] = online_month_df['GMV'] / online_month_df['房量']
    # online_month_df['套均净利'] = online_month_df['总净利'] / online_month_df['房量']

    online_month_df['套均GMV'] = online_month_df.apply(lambda row: divi(row, 'GMV', '房量'), axis=1)
    online_month_df['套均净利'] = online_month_df.apply(lambda row: divi(row, '总净利', '房量'), axis=1)
    city_month_df = online_month_df[['城市', '门店销售', '可租率', '入住率', '预定订单', '预定订单间夜', '日价',
                                     'REVPAR', 'GMV', '总净利', '套均GMV', '套均净利']]

    log.info("月累计销售数据准备完毕。。。。。。。")
    im_online_month_df = im_online_day(st=st, et=yesterday)
    log.info("月累计im数据准备完毕。。。。。。。")
    month_df = pd.merge(city_month_df, im_online_month_df, how='left')
    log.info("合并月累计im数据、月累计销售数据完毕。。。。。。。")
    month_df = df_util.df_set_index(month_df, ["城市", "门店销售"])
    month_df = pd.merge(month_df, month_order_df, how='left', left_index=True, right_index=True)
    log.info("合并月累计im数据、月累计销售数据、月累计评论完毕。。。。。。。")

    refund_df = online_refund_rate_month(st=st, et=yesterday)
    refund_df = refund_df.reset_index()
    refund_df = refund_df.rename(columns={'销售': '门店销售'})
    refund_df = df_util.df_set_index(refund_df, ["城市", "门店销售"])
    refund_df = refund_df[['退款率']]
    month_df = pd.merge(month_df, refund_df, how='left', left_index=True, right_index=True)

    month_df = df_util.df_set_first_title(month_df, "累计数据")
    result_df = pd.merge(result_df, month_df, how='left', left_index=True, right_index=True)
    # result_df = pd.merge(result_df, airbnb_df, how='left', left_index=True, right_index=True)
    # result_df = result_df.reset_index()
    # result_df = df_util.df_set_index(result_df, '城市')

    df_dict = {'经营日报-销售': result_df}
    colors_dict = {'经营日报-销售': DAY_REPORT_BUSINESS_COLOR}
    #
    text = """    Dear All:
                    附件是 经营日报-销售！
                    请查收！
                    谢谢！
            如有问题！
            请联系: dt@iyoujia.com
            """
    mail_util.distribute_mail(df_dict=df_dict, file_name='经营日报-销售', text=text, engine='xlsxwriter',
                              html_sheet_name='经营日报-销售',
                              title="【经营日报-销售】", colors_dict=colors_dict, receiver=receivers, na_rep='-',
                              uuid='city', freeze_panes=(2, 2))


if __name__ == '__main__':
    process()
